// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database();
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  let key = event.key
  let res = await db.collection('song').where(_.or([{
      title: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    },
    {
      singer: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    },
    {
      pickerInfo: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    },
    {
      tip: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    }
  ])).orderBy('time', 'desc').get()
  return res
}